﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
    public class BST<E> where E : IComparable<E>
    {
        private class Node
        {
            public E e;
            public Node left, right;
            public Node(E e)
            {
                this.e = e;
                left = null;
                right = null;
            }
        }

        private Node root;
        private int size;

        public BST()
        {
            root = null;
            size = 0;
        }

        public int Size()
        {
            return size;
        }

        public bool isEmpty()
        {
            return size == 0;
        }

        #region 插入
        // 向二分搜索树中添加新的元素e
        public void add(E e)
        {
            //if (root == null)
            //{
            //    root = new Node(e);
            //    size++;
            //}
            //else
            //    add(root, e);

            root = add(root, e);
        }

        #region 臃肿插入
        // 向以node为根的二分搜索树中插入元素e，递归算法
        //private void add(Node node, E e)
        //{
        //    if (e.Equals(node.e))
        //        return;
        //    else if (e.CompareTo(node.e) < 0 && node.left == null)
        //    {
        //        node.left = new Node(e);
        //        size++;
        //        return;
        //    }
        //    else if (e.CompareTo(node.e) > 0 && node.right == null)
        //    {
        //        node.right = new Node(e);
        //        size++;
        //        return;
        //    }

        //    if (e.CompareTo(node.e) < 0)
        //        add(node.left, e);
        //    else //e.compareTo(node.e) > 0
        //        add(node.right, e);
        //}
        #endregion

        // 向以node为根的二分搜索树中插入元素e，递归算法
        // 返回插入新节点后二分搜索树的根
        private Node add(Node node, E e)
        {
            if (node == null)
            {
                size++;
                return new Node(e);
            }

            if (e.CompareTo(node.e) < 0)
                node.left = add(node.left, e);
            else if (e.CompareTo(node.e) > 0)
                node.right = add(node.right, e);

            return node;
        }
        #endregion

        #region 查询       
        //看二分搜索树中是否包含元素e
        public bool contains(E e)
        {
            return contains(root, e);
        }
        private bool contains(Node node, E e)
        {
            if (node == null)
                return false;

            if (e.CompareTo(node.e) == 0)
                return true;
            else if (e.CompareTo(node.e) < 0)
                return contains(node.left, e);
            else //(e.CompareTo(node.e) > 0)
                return contains(node.right, e);
        }

        //获取最小元素
        public E minnum()
        {
            if (size == 0)
                throw new Exception("二分搜索树为空");
            return minnum(root).e;
        }

        private Node minnum(Node node)
        {
            return node.left == null ? node : minnum(node.left);
        }

        //获取最大元素
        public E maxnum()
        {
            if (size == 0)
                throw new Exception("二分搜索树为空");
            return maxnum(root).e;
        }

        private Node maxnum(Node node)
        {
            return node.right == null ? node : maxnum(node.right);
        }

        #endregion

        #region 遍历
        //递归实现 前中后序遍历
        public void Order()
        {
            Order(root);
        }
        private void Order(Node node)
        {
            if (node == null)
                return;
            //Console.WriteLine(node.e);//前序遍历

            Order(node?.left);

            Console.WriteLine(node.e);//中序遍历

            Order(node?.right);

            // Console.WriteLine(node.e);//后序遍历

        }

        //二分搜索树的层序遍历
        public void leveOrde()
        {
            Queue<Node> q = new Queue<Node>();
            q.Enqueue(root);
            while (q.Count > 0)
            {
                Node cur = q.Dequeue();
                Console.WriteLine(cur.e);
                if (cur.left != null)
                    q.Enqueue(cur.left);
                if (cur.right != null)
                    q.Enqueue(cur.right);
            }
        }

        // 二分搜索树的非递归前序遍历
        public void OrderNR()
        {

            Stack<Node> stack = new Stack<Node>();
            stack.Push(root);
            while (stack.Count > 0)
            {
                Node cur = stack.Pop();
                Console.WriteLine(cur.e);

                if (cur.right != null)
                    stack.Push(cur.right);
                if (cur.left != null)
                    stack.Push(cur.left);
            }
        }

        #endregion

        #region 删除
        // 从二分搜索树中删除最小值所在节点, 返回最小值
        public E removeMin()
        {
            E ret = minnum();
            root = removeMin(root);
            return ret;
        }

        // 删除掉以node为根的二分搜索树中的最小节点
        // 返回删除节点后新的二分搜索树的根
        private Node removeMin(Node node)
        {
            if (node.left == null)
            {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }

            node.left = removeMin(node.left);
            return node;
        }

        // 从二分搜索树中删除最大值所在节点
        public E removeMax()
        {
            E ret = maxnum();
            root = removeMax(root);
            return ret;
        }

        // 删除掉以node为根的二分搜索树中的最大节点
        // 返回删除节点后新的二分搜索树的根
        private Node removeMax(Node node)
        {

            if (node.right == null)
            {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }

            node.right = removeMax(node.right);
            return node;
        }

        // 从二分搜索树中删除元素为e的节点
        public void remove(E e)
        {
            root = remove(root, e);
        }

        // 删除掉以node为根的二分搜索树中值为e的节点, 递归算法
        // 返回删除节点后新的二分搜索树的根
        private Node remove(Node node, E e)
        {
            if (node == null)
                return null;

            if (e.CompareTo(node.e) < 0)
            {
                node.left = remove(node.left, e);
                return node;
            }
            else if (e.CompareTo(node.e) > 0)
            {
                node.right = remove(node.right, e);
                return node;
            }
            else
            {   // e.CompareTo(node.e) == 0

                // 待删除节点左子树为空的情况
                if (node.left == null)
                {
                    Node rightNode = node.right;
                    node.right = null;
                    size--;
                    return rightNode;
                }

                // 待删除节点右子树为空的情况
                if (node.right == null)
                {
                    Node leftNode = node.left;
                    node.left = null;
                    size--;
                    return leftNode;
                }

                // 待删除节点左右子树均不为空的情况
                // 找到比待删除节点大的最小节点, 即待删除节点右子树的最小节点
                // 用这个节点顶替待删除节点的位置
                Node successor = minnum(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;

                node.left = node.right = null;

                return successor;
            }
        }
        #endregion

        #region 打印输出       
        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            generateBSTString(root, 0, res);
            return res.ToString();
        }

        // 生成以node为根节点，深度为depth的描述二叉树的字符串
        private void generateBSTString(Node node, int depth, StringBuilder res)
        {

            if (node == null)
            {
                res.Append(generateDepthString(depth) + "null\n");
                return;
            }

            res.Append(generateDepthString(depth) + node.e + "\n");
            generateBSTString(node.left, depth + 1, res);
            generateBSTString(node.right, depth + 1, res);
        }

        private string generateDepthString(int depth)
        {
            StringBuilder res = new StringBuilder();
            for (int i = 0; i < depth; i++)
                res.Append("--");
            return res.ToString();
        }
        #endregion
    }
}
